using MaterialSkin.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;
using VideoAutoGenerator.UI.UserControls;

namespace VideoAutoGenerator.UI.Forms.ConfigForms
{
    public partial class ConfigurationForm : Form
    {
        private MaterialTabControl tabControl;

        // 用户控件
        private BasicSettingsControl basicSettingsControl;
        private ProjectSettingsControl projectSettingsControl;
        private FontSettingsControl fontSettingsControl;
        private WaveformSettingsControl waveformSettingsControl;

        public ConfigurationForm()
        {
            InitializeComponent();
            
            // Load configuration data into controls
            LoadConfigData();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();

            // 
            // ConfigurationForm
            // 
            this.ClientSize = new System.Drawing.Size(900, 700);
            this.MaximizeBox = false;
            this.Name = "ConfigurationForm";
            this.Text = "配置管理";

            // Create tab control for different config sections
            tabControl = new MaterialTabControl();
            tabControl.MouseState = MaterialSkin.MouseState.HOVER;
            tabControl.Multiline = true;
            tabControl.Depth = 0;
            tabControl.Dock = DockStyle.Fill;

            // Create tab selector
            MaterialTabSelector tabSelector = new MaterialTabSelector
            {
                Dock = DockStyle.Top,
                BaseTabControl = tabControl,
                Height = 50
            };
            // Create tabs
            TabPage basicTab = new TabPage("基本配置");
            basicTab.Padding = new Padding(10, 20, 10, 10); // 增加上边距避免被TabSelector遮挡
            basicTab.BackColor = Color.White;
            basicTab.AutoScroll = true;  // 启用自动滚动条
            basicTab.BorderStyle = BorderStyle.None; // 去除边框避免遮挡
            
            TabPage projectTab = new TabPage("项目设置");
            projectTab.Padding = new Padding(10, 20, 10, 10); // 增加上边距避免被TabSelector遮挡
            projectTab.BackColor = Color.White;
            projectTab.AutoScroll = true;  // 启用自动滚动条
            projectTab.BorderStyle = BorderStyle.None; // 去除边框避免遮挡
            
            TabPage fontTab = new TabPage("字体样式");
            fontTab.Padding = new Padding(10, 20, 10, 10); // 增加上边距避免被TabSelector遮挡
            fontTab.BackColor = Color.White;
            fontTab.AutoScroll = true;  // 启用自动滚动条
            fontTab.BorderStyle = BorderStyle.None; // 去除边框避免遮挡
            
            TabPage waveformTab = new TabPage("波形显示");
            waveformTab.Padding = new Padding(10, 20, 10, 10); // 增加上边距避免被TabSelector遮挡
            waveformTab.BackColor = Color.White;
            waveformTab.AutoScroll = true;  // 启用自动滚动条
            waveformTab.BorderStyle = BorderStyle.None; // 去除边框避免遮挡

            // Create user controls
            basicSettingsControl = new BasicSettingsControl();
            projectSettingsControl = new ProjectSettingsControl();
            fontSettingsControl = new FontSettingsControl();
            waveformSettingsControl = new WaveformSettingsControl();

            // Set dock style for user controls
            basicSettingsControl.Dock = DockStyle.Fill;
            projectSettingsControl.Dock = DockStyle.Fill;
            fontSettingsControl.Dock = DockStyle.Fill;
            waveformSettingsControl.Dock = DockStyle.Fill;

            // Add controls to tabs
            basicTab.Controls.Add(basicSettingsControl);
            projectTab.Controls.Add(projectSettingsControl);
            fontTab.Controls.Add(fontSettingsControl);
            waveformTab.Controls.Add(waveformSettingsControl);

            // Add tabs to tab control
            tabControl.TabPages.Add(basicTab);
            tabControl.TabPages.Add(projectTab);
            tabControl.TabPages.Add(fontTab);
            tabControl.TabPages.Add(waveformTab);

            // Create button panel
            Panel buttonPanel = new MaterialSkin.Controls.MaterialCard();
            buttonPanel.Height = 60;
            buttonPanel.Dock = DockStyle.Bottom;
            buttonPanel.BackColor = Color.FromArgb(245, 245, 245);

            // Create buttons
            Button saveButton = new Button();
            saveButton.Text = "保存配置";
            saveButton.Size = new Size(100, 35);
            saveButton.Location = new Point(buttonPanel.Width - 220, 15);
            saveButton.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            saveButton.Click += SaveButton_Click;

            Button resetButton = new Button();
            resetButton.Text = "重置";
            resetButton.Size = new Size(100, 35);
            resetButton.Location = new Point(buttonPanel.Width - 110, 15);
            resetButton.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            resetButton.Click += ResetButton_Click;

            // Add buttons to panel
            buttonPanel.Controls.Add(saveButton);
            buttonPanel.Controls.Add(resetButton);

            // Add controls to form
            this.Controls.Add(tabControl);
            this.Controls.Add(buttonPanel);
            this.Controls.Add(tabSelector); // 确保tabSelector在最后添加，显示在最上层

            this.ResumeLayout(false);
        }
        
        // Load configuration data into all controls
        private void LoadConfigData()
        {
            basicSettingsControl.LoadConfig();
            projectSettingsControl.LoadConfig();
            fontSettingsControl.LoadConfig();
            waveformSettingsControl.LoadConfig();
        }
        
        // Save configuration data from all controls
        private void SaveButton_Click(object sender, EventArgs e)
        {
            basicSettingsControl.SaveConfig();
            projectSettingsControl.SaveConfig();
            fontSettingsControl.SaveConfig();
            waveformSettingsControl.SaveConfig();
            
            MessageBox.Show("配置已保存！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        
        // Reset configuration data to default values
        private void ResetButton_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确定要重置所有配置为默认值吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                // TODO: Implement reset functionality
                MessageBox.Show("配置已重置为默认值！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}